var e = {
    request: function(t) {
        var n = getApp(), o = (t = t || {}).url;
        if (-1 == o.indexOf("http://") && -1 == o.indexOf("https://") && (o = n.globalData.siteroot + t.url), 
        !o) return !1;
        t.loading = void 0 === t.loading || t.loading, t.loading && (wx.showNavigationBarLoading(), 
        wx.showLoading({
            title: "加载中"
        }));
        var a = t.data ? t.data : {};
        return n.globalData.site_id && (a.site_id = n.globalData.site_id), new Promise(function(n, i) {
            wx.request({
                url: o,
                data: a,
                method: t.method ? t.method : "POST",
                dataType: "json",
                header: {
                    "Content-Type": "application/x-www-form-urlencoded",
                    "X-Token": wx.getStorageSync("token")
                },
                timeout: t.timeout ? t.timeout : 6e4,
                success: function(o) {
                    if (200 == o.statusCode) if (o.data.errno > 0) {
                        if (403 === o.data.errno) return void ("/wxapp/checkLogin" == t.url ? i(o) : e.login().then(function() {
                            e.request(t).then(function(e) {
                                n(e);
                            });
                        }));
                        o.data.message && e.message(o.data.message, "error"), i(o.data);
                    } else n(o.data); else i(o);
                },
                fail: function(t) {
                    e.message("网络错误", "error"), i(t);
                },
                complete: function(e) {
                    t.loading && (wx.hideLoading(), wx.hideNavigationBarLoading());
                }
            });
        });
    },
    upload: function() {
        var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : null;
        return function(t) {
            var n = getApp(), o = (t = t || {}).url;
            if (-1 == o.indexOf("http://") && -1 == o.indexOf("https://") && (o = n.globalData.siteroot + t.url), 
            !o) return !1;
            t.loading = void 0 === t.loading || t.loading, t.loading && (wx.showNavigationBarLoading(), 
            wx.showLoading({
                title: "上传中"
            }));
            var a = t.data ? t.data : {};
            return n.globalData.site_id && (a.site_id = n.globalData.site_id), new Promise(function(n, a) {
                wx.uploadFile({
                    url: o,
                    data: t.data ? t.data : {},
                    filePath: t.filePath,
                    name: t.name ? t.name : "image",
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded",
                        "X-Token": wx.getStorageSync("token")
                    },
                    dataType: "json",
                    success: function(t) {
                        var o = JSON.parse(t.data);
                        o.errno > 0 ? 403 === o.errno ? wx.navigateTo({
                            url: "/pages/login/login"
                        }) : (o.message && e.message(o.message, "error"), a(o)) : n(o);
                    },
                    fail: function(t) {
                        e.message("网络错误", "error"), a(t);
                    },
                    complete: function(e) {
                        t.loading && (wx.hideLoading(), wx.hideNavigationBarLoading());
                    }
                });
            });
        }(t);
    },
    login: function() {
        return new Promise(function(t, n) {
            var o = wx.getStorageSync("sid");
            wx.login({
                success: function(n) {
                    var a = n.code;
                    e.request({
                        url: "/wxapp/login",
                        data: {
                            code: a,
                            sid: parseInt(o)
                        }
                    }).then(function(e) {
                        wx.setStorageSync("token", e.data.token), o && wx.removeStorageSync("sid"), t();
                    }).catch(function(t) {
                        e.message(t.message, "error", function() {
                            setTimeout(function() {
                                e.login();
                            }, 1e3);
                        });
                    });
                }
            });
        });
    },
    checkLogin: function() {
        return new Promise(function(t, n) {
            e.request({
                url: "/wxapp/checkLogin",
                loading: !1
            }).then(function() {
                t();
            }).catch(function(o) {
                e.login().then(function() {
                    t();
                }).catch(function() {
                    n();
                });
            });
        });
    },
    message: function(e) {
        var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "none", n = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : null;
        if (!e) return !0;
        "success" == t ? wx.showToast({
            title: e,
            icon: "success",
            duration: 2e3,
            mask: !1,
            complete: function() {
                n && "function" == typeof n && setTimeout(function() {
                    n();
                }, 1800);
            }
        }) : "none" == t ? wx.showToast({
            title: e,
            icon: "none",
            duration: 2e3,
            mask: !1,
            complete: function() {
                n && "function" == typeof n && setTimeout(function() {
                    n();
                }, 1800);
            }
        }) : "error" == t && wx.showModal({
            title: "系统提示",
            content: e,
            showCancel: !1,
            complete: function() {
                n && "function" == typeof n && n();
            }
        });
    },
    queryToArr: function(e) {
        var t = [], n = [], o = e.split("&");
        for (var a in o) t[(n = o[a].split("="))[0]] = n[1];
        return t;
    }
};

module.exports = e;